package sg.edu.nus.iss.umpire.service.workflow.dto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sg.edu.nus.iss.umpire.domain.CompetitionDay;
import sg.edu.nus.iss.umpire.domain.Session;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


public class CompetitionDayDTO {
	
	private String date;
	
	private boolean morning;
	private boolean afternoon;
	private boolean evening;
	
	//secondary attributes
	private SessionDTO morningSession;
	private SessionDTO afternoonSession;
	private SessionDTO eveningSession;
	
	
	public SessionDTO getMorningSession() {
		return morningSession;
	}
	public void setMorningSession(SessionDTO morningSession) {
		this.morningSession = morningSession;
	}
	public SessionDTO getAfternoonSession() {
		return afternoonSession;
	}
	public void setAfternoonSession(SessionDTO afternoonSession) {
		this.afternoonSession = afternoonSession;
	}
	public SessionDTO getEveningSession() {
		return eveningSession;
	}
	public void setEveningSession(SessionDTO eveningSession) {
		this.eveningSession = eveningSession;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public boolean isMorning() {
		return morning;
	}
	public void setMorning(boolean morning) {
		this.morning = morning;
	}
	public boolean isAfternoon() {
		return afternoon;
	}
	public void setAfternoon(boolean afternoon) {
		this.afternoon = afternoon;
	}
	public boolean isEvening() {
		return evening;
	}
	public void setEvening(boolean evening) {
		this.evening = evening;
	}
	
	public static List<CompetitionDayDTO> fromJSON(String json){
		Gson gson = new Gson();
    	return gson.fromJson(json, new TypeToken<List<CompetitionDayDTO>>(){}.getType());
	}
	
	
	public static List<CompetitionDayDTO> fromEntity(List<CompetitionDay> days) {
		List<CompetitionDayDTO> result = new ArrayList<CompetitionDayDTO>();
		for (CompetitionDay day : days) {
			CompetitionDayDTO dto = new CompetitionDayDTO();
			dto.setDate(day.getDate());
			Session morningSes = find(day.getSessions(), Session.SLOT_MORNING);
			dto.setMorning(morningSes != null);
			dto.setMorningSession(SessionDTO.fromEntity(morningSes));
			
			Session afternoonSes = find(day.getSessions(), Session.SLOT_AFTERNOON);
			dto.setAfternoon(afternoonSes != null);
			dto.setAfternoonSession(SessionDTO.fromEntity(afternoonSes));
			
			
			Session eveningSes = find(day.getSessions(), Session.SLOT_EVENING);
			dto.setEvening(eveningSes != null);
			dto.setEveningSession(SessionDTO.fromEntity(eveningSes));
			
			result.add(dto);
		}
		return result;
	}
	
	private static Session find(List<Session> sessions, String name) {
		for (Session session : sessions) {
			if (session.getSlotName().equalsIgnoreCase(name)){
				return session;
			}
		}
		
		return null;
	}
	
	public static List<CompetitionDayDTO> group(List<SessionDTO> sessions){
		Map<String, CompetitionDayDTO> map = new HashMap<String, CompetitionDayDTO>();
		for (SessionDTO session : sessions) {
			String date = session.getDate();
			if (!map.containsKey(date)){
				CompetitionDayDTO day = new CompetitionDayDTO();
				day.setDate(date);
				map.put(date, day);
			}
			
			CompetitionDayDTO day = map.get(date);
			if(session.getSlotName().equals(Session.SLOT_MORNING)){
				day.setMorning(true);
			}else if(session.getSlotName().equals(Session.SLOT_AFTERNOON)){
				day.setAfternoon(true);
			}else if(session.getSlotName().equals(Session.SLOT_EVENING)){
				day.setEvening(true);
			}
		}
		
		return new ArrayList(map.values());
    	
	}
	
}
